package com.spring.oaassist.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.spera.shopoa.util.ShopOaSessionUtil;
import com.spring.common.web.model.CareClassAppointment;
import com.spring.common.web.model.CourseAppointment;
import com.spring.cootask.web.model.FeTaskUserWithBLOBs;
import com.spring.oaassist.web.form.CourseForm;
import com.spring.oaassist.web.form.CourseQueryForm;
import com.spring.oaassist.web.service.CourseService;
import com.spring.security.core.annotation.OaUserPermission;
import com.spring.security.core.validators.Insert;
import com.spring.security.core.validators.Update;
import com.spring.tools.Result;
import com.spring.tools.ValidatorResultHandler;

/**
 * 课程相关
 * @author wojunyu
 *
 */
@Controller
@RequestMapping(value = "/course")
public class CourseController {
	@Resource
	private CourseService courseService;
	
	/**
	 * 早教课程预约
	 * @param courseForm
	 * @return
	 */
	@RequestMapping(value = "/appointCourse")
	@ResponseBody
    public Object appointCourse(@Validated({Insert.class}) CourseForm courseForm,BindingResult result) {
		Result retVal = ValidatorResultHandler.handle(result);
		if("0".equalsIgnoreCase(retVal.getStatus())){
			return retVal;
		}
		try {
			retVal  = courseService.appointCourse(courseForm);
		} catch (Exception e) {
			retVal.setStatus("0");
			retVal.setMsg("系统异常");
			e.printStackTrace();
		}
        return retVal;
    }	
	
	/**
	 * 早教取消预约
	 * @param token
	 * @param course_appointmet_id
	 * @param callback
	 * @return
	 */
	@RequestMapping(value = "/cancelCourse")
	@ResponseBody
	public Object cancelCourse(String course_appointmet_id) {
		Result m = new Result();
		try {
			m = courseService.cancelCourse(course_appointmet_id);
		} catch (Exception e) {
			m.setStatus("0");
			m.setMsg("系统异常");
			e.printStackTrace();
		}
		return m;
	}
	
	
	/**
	 * 托班预约
	 * @param courseForm
	 * @return
	 */
	@RequestMapping(value = "/appointCareClass")
	@ResponseBody
	public Object appointCareClass(@Validated({Update.class}) CourseForm courseForm,BindingResult result) {
		Result retVal = ValidatorResultHandler.handle(result);
		if("0".equalsIgnoreCase(retVal.getStatus())){
			return retVal;
		}
		try {
			retVal  = courseService.appointCareClass(courseForm);
		} catch (Exception e) {
			retVal.setStatus("0");
			retVal.setMsg("系统异常");
			e.printStackTrace();
		}
        return retVal;
	}
	
	/**
	 * 
	 * @param courseForm
	 * @param result
	 * @return
	 */
	@RequestMapping(value = "/releaseCourse")
	@ResponseBody
	public Object releaseCourse(@RequestParam String id,@RequestParam String course_type,@RequestParam String isReleased) {
		Result retVal = new Result();
		try {
			retVal  = courseService.releaseCourse(id,course_type,isReleased);
		} catch (Exception e) {
			retVal.setStatus("0");
			retVal.setMsg("系统异常");
			e.printStackTrace();
		}
        return retVal;
	}

	
	/**
	 * 托班取消预约
	 * @param c_user_id
	 * @param care_class_appointment_id
	 * @return
	 */
	@RequestMapping(value = "/cancelCareClass")
	@ResponseBody
	public Object cancelCareClass(String care_class_appointment_id) {
		Result retVal = new Result();
		try {
			retVal = courseService.cancelCareClass(care_class_appointment_id);
		} catch (Exception e) {
			retVal.setStatus("0");
			retVal.setMsg("系统异常");
			e.printStackTrace();
		}
		return retVal;
	}		

	/**
	 * 查看自己的托班预约/签到记录
	 * @param queryForm
	 * @return
	 */
	@RequestMapping(value = "/getMyCareClassList")
	@ResponseBody
	public Object getMyCareClassList(CourseQueryForm queryForm) {
		Result retVal = new Result();
		return retVal;
	}

	@RequestMapping(value = "/studentList")
	public ModelAndView studentList(String course_open_detailID,String course_type,Model model,HttpServletRequest request) {
	    model.addAttribute("course_open_detailID",course_open_detailID);
	    //早教1  托班2
	    model.addAttribute("course_type",course_type);
		return new ModelAndView("main/workschedule/studentList");
	}
	
	@RequestMapping(value = "/student",method = RequestMethod.GET)
	@ResponseBody
	public Object student(String course_open_detailID,String page,String rows,HttpServletRequest request){
	    Map<String, Object> retD = new HashMap<String, Object>();
    	PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(rows));
    	List<CourseAppointment> list = courseService.student(course_open_detailID);
        PageInfo<CourseAppointment> pageObj = new PageInfo<CourseAppointment>(list);
        retD.put("total", pageObj.getTotal());
        retD.put("rows", list); 	
        if(list.size()==0){
        	PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(rows));
        	List<CareClassAppointment> list2 = courseService.careClassStudent(course_open_detailID);
            PageInfo<CareClassAppointment> pageObj2 = new PageInfo<CareClassAppointment>(list2);
            retD.put("total", pageObj2.getTotal());
            retD.put("rows", list2); 
        }
	    return retD;
	}
}
